package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AnnounceDao;
import dao.UserDao;
import dao.impl.AnnounceDaoImpl;
import dao.impl.UserDaoImpl;
import model.Announces;
import model.User;

/**
 * Servlet implementation class AnnServlet
 */
@WebServlet("/AnnServlet")
public class AnnServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AnnServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		//取得页面中的参数
		String action = request.getParameter("action");
		//把参数转换为内容ID
		int classID = 0;		
		//调用数据访问层
		AnnounceDao ad = new AnnounceDaoImpl();
		UserDao ud = new UserDaoImpl();
		List<Announces> annList = new ArrayList<>();
		try {		
			//得到所有用户列表信息
//			request.setAttribute("allUserList", ud.getAllUser());
			if(action.equals("reading")) {
				classID = 3 ;
				annList = ad.queryAnnListByClassID(classID);
				request.setAttribute("AnnList",annList);
				//根据文学类别下的主题列表信息得到此主题下的用户列表
				request.setAttribute("readUserList", ud.getUserListByAnn(annList));
				request.getRequestDispatcher("pages/module/reading.jsp").forward(request, response);
			}else if(action.equals("music")) {
				classID = 1 ;
				annList = ad.queryAnnListByClassID(classID);
				request.setAttribute("AnnList",annList);
				//根据音乐类别下的主题列表信息得到此主题下的用户列表
				request.setAttribute("readUserList", ud.getUserListByAnn(annList));
				request.getRequestDispatcher("pages/module/music.jsp").forward(request, response);
			}else if(action.equals("film")) {
				classID = 2 ;
				annList = ad.queryAnnListByClassID(classID);
				request.setAttribute("AnnList",annList);
				//根据电影类别下的主题列表信息得到此主题下的用户列表
				request.setAttribute("readUserList", ud.getUserListByAnn(annList));
				request.getRequestDispatcher("pages/module/film.jsp").forward(request, response);
			}			
			//response.sendRedirect("IndexServlet");
			//request.getRequestDispatcher("pages/home.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
